home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / ccalc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  5.9 KB  |  262 lines

  1. /********************************************************************/
  2. /*                                                                  */
  3. /*    MODUL              : CCALC.C                                  */
  4. /*    VERSION            : 1.0                                      */
  5. /*    DATE               : 24/09/91                                 */
  6. /*    WRITTEN BY         : Christoph Bohle                          */
  7. /* ---------------------------------------------------------------- */
  8. /*    LANGUAGE           : C                                        */
  9. /*    OS                 : UNIX                                     */
  10. /*                                                                  */
  11. /********************************************************************/
  12.  
  13. /********************************************************************/
  14. /*   INCLUDE-Files                                                  */
  15. /********************************************************************/
  16.  
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <stdlib.h>
  22.               
  23. /********************************************************************/
  24. /*    Functions                                                     */
  25. /********************************************************************/
  26.  
  27.  
  28. float expression();
  29.  
  30. char *end_of_brackets( str )
  31. char *str;  
  32. {                                   
  33.   int i;
  34.   i=1;
  35.   do
  36.   {    
  37.     str++;
  38.     switch( *str )
  39.     {
  40.       case '('  : i++;
  41.                   break;
  42.       case ')'  : i--;
  43.                       break;
  44.       case '\0' : i=0;
  45.     }
  46.   }
  47.   while( i>0 );
  48.   return( str );
  49. }
  50.         
  51. float factor( facstr )
  52. char *facstr;
  53. {
  54.   char *help, c;
  55.   
  56.   /* Float */
  57.   if( isdigit(*facstr) || *facstr=='.' || *facstr=='-' || *facstr=='+' )
  58.     return( atof(facstr) );
  59.   
  60.   /* Expression in brackets */
  61.   if( *facstr=='(' )
  62.   {
  63.     *(end_of_brackets( ++facstr )) = '\0';
  64.     return( expression( facstr ) );
  65.   }
  66.   
  67.   /* Function */
  68.   help = facstr;
  69.   while( isalnum(*help) || *help == '_' || *help == '.' )
  70.     help++;
  71.   if( help != facstr )
  72.   {
  73.     c = *help;
  74.     *help = '\0';
  75.     
  76.     /* Function */
  77.     if( strcmp( facstr , "exp" ) == 0 )
  78.     {
  79.       *help = c;
  80.       return( exp( factor( help ) ) );
  81.     }
  82.     if( strcmp( facstr , "log" ) == 0 )
  83.     {
  84.       *help = c;
  85.       return( log( factor( help ) ) );
  86.     }
  87.     if( strcmp( facstr , "log10" ) == 0 )
  88.     {
  89.       *help = c;
  90.       return( log10( factor( help ) ) );
  91.     }
  92.     if( strcmp( facstr , "sin" ) == 0 )
  93.     {
  94.       *help = c;
  95.       return( sin( factor( help ) ) );
  96.     }
  97.     if( strcmp( facstr , "cos" ) == 0 )
  98.     {
  99.       *help = c;
  100.       return( cos( factor( help ) ) );
  101.     }
  102.     if( strcmp( facstr , "tan" ) == 0 )
  103.     {
  104.       *help = c;
  105.       return( tan( factor( help ) ) );
  106.     }
  107.     if( strcmp( facstr , "asin" ) == 0 )
  108.     {
  109.       *help = c;
  110.       return( asin( factor( help ) ) );
  111.     }
  112.     if( strcmp( facstr , "acos" ) == 0 )
  113.     {
  114.       *help = c;
  115.       return( acos( factor( help ) ) );
  116.     }
  117.     if( strcmp( facstr , "atan" ) == 0 )
  118.     {
  119.       *help = c;
  120.       return( atan( factor( help ) ) );
  121.     }
  122.     if( strcmp( facstr , "sinh" ) == 0 )
  123.     {
  124.       *help = c;
  125.       return( sinh( factor( help ) ) );
  126.     }
  127.     if( strcmp( facstr , "cosh" ) == 0 )
  128.     {
  129.       *help = c;
  130.       return( cosh( factor( help ) ) );
  131.     }
  132.     if( strcmp( facstr , "tanh" ) == 0 )
  133.     {
  134.       *help = c;
  135.       return( tanh( factor( help ) ) );
  136.     }
  137.   }
  138.   
  139.   /* Default */
  140.   if( *facstr == '\0' )
  141.     return( 0.0 );
  142.   else
  143.     return( factor( facstr+1 ) ); 
  144. }
  145.  
  146. float po( postr )
  147. char postr[];
  148. {
  149.   char *help;
  150.   help = postr;
  151.   
  152.   while( (*help != '^') && (*help != '\0' ) )
  153.   {
  154.     if( *help == '(' )
  155.         help = end_of_brackets( help );
  156.     help++;
  157.   }
  158.   
  159.   switch( *help )
  160.   {
  161.       case '^' : *help = '\0';
  162.                return( pow( factor( postr ) , po( help+1 ) ));
  163.     case '\0' : if( help == postr ) 
  164.                   return( 0.0 );
  165.                 else
  166.                   return( factor( postr ) );
  167.   }
  168. }
  169.  
  170. float term( termstr )
  171. char termstr[];
  172. {
  173.   char *help;
  174.   help = termstr;
  175.   
  176.   while( (*help != '*') && (*help != '/') && (*help != '\0' ) )
  177.   {
  178.     if( *help == '(' )
  179.         help = end_of_brackets( help );
  180.     help++;
  181.   }
  182.   
  183.   switch( *help )
  184.   {
  185.       case '*' : *help = '\0';
  186.                return( po( termstr ) * term( help+1 ) );
  187.     case '/' : *help = '\0';
  188.                return( po( termstr ) / term( help+1 ) );
  189.     case '\0' : if( help == termstr ) 
  190.                   return( 0.0 );
  191.                 else
  192.                   return( po( termstr ) );
  193.   }
  194. }
  195.  
  196. float expression( exprstr )
  197. char exprstr[];
  198. {
  199.   char *help;
  200.   help = exprstr;
  201.   
  202.   while( (*help != '+') && (*help != '-') && (*help != '\0' ) )
  203.   {
  204.     if( *help == '(' )
  205.         help = end_of_brackets( help );
  206.     help++;
  207.   }
  208.   
  209.   switch( *help )
  210.   {
  211.       case '+' : *help = '\0';
  212.                return( term( exprstr ) + expression( help+1 ) );
  213.     case '-' : *help = '\0';
  214.                return( term( exprstr ) - expression( help+1 ) );
  215.     case '\0' : if( help == exprstr ) 
  216.                   return( 0.0 );
  217.                 else
  218.                   return( term( exprstr ) );
  219.   }
  220. }
  221.  
  222. float analyse_function( funstr )
  223. char *funstr;
  224. {
  225.   char *help;
  226.   float res;
  227.   
  228.   help=malloc( strlen(funstr)+1 );
  229.   strcpy( help , funstr );
  230.   res = expression( help );
  231.   free( help );
  232.   return( res );  
  233. }
  234.  
  235. /********************************************************************/
  236. /* Main-function                                                    */
  237. /********************************************************************/
  238.  
  239. main( argc , argv  )
  240. int argc;
  241. char *argv[];              
  242. {
  243.   int i, l;
  244.   char *fun;
  245.   
  246.   l = 1;
  247.   for( i=1 ; i < argc ; i++ )
  248.   {
  249.     l += strlen( argv[i] ) + 1;
  250.   }
  251.   fun = malloc( l+3 ); *fun = '\0';
  252.   for( i=1 ; i < argc ; i++ )
  253.   {
  254.     strcat( fun , argv[i] );
  255.     strcat( fun , " ");
  256.   }
  257.   printf("%f\n", (double)analyse_function( fun ) );
  258.   return(0);
  259. }
  260.  
  261. /********************************************************************/
  262.